#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/9/12 11:07
# ===========================================
#       题目名称： 55. 跳跃游戏
#       题目地址： https://leetcode.cn/problems/jump-game/
#       题目描述： https://note.youdao.com/s/VjIUrvCD
# ===========================================
from typing import List


class Solution:
    def canJump(self, nums: List[int]) -> bool:
        # 判断是否是空的元素
        if not nums:
            return False
        # 判断首位是否是0
        if nums[0] == 0:
            # 如果只有一条数据 则表示合规
            if len(nums) == 1:
                return True
            else:
                return False
        energy = nums[0]  # 能量
        is_to_end = True  # 是否到下一个
        for index in range(1, len(nums)):
            if nums[index] >= energy:
                energy = nums[index]
            else:
                energy -= 1
            # 能量消耗完 没到终点
            if energy <= 0 and index != len(nums) - 1:
                is_to_end = False
                break
        return is_to_end

    def canJump2(self, nums: List[int]) -> bool:
        """
            实现有误
        """
        step = 1  # 默认有一步
        cur_index = 0  # 当前下标
        jump_to_last = False  # 跳跃到最后
        while cur_index < len(nums):
            next_step = nums[step]
            if cur_index + next_step + 1 >= len(nums):
                jump_to_last = True
                break
            if nums[step] == 0:
                break
            cur_index += next_step
            step = cur_index
        return jump_to_last


if __name__ == '__main__':
    s = Solution()
    # True
    print(s.canJump(nums=[2, 3, 1, 1, 4]))
    # False
    print(s.canJump(nums=[3, 2, 1, 0, 4]))
    # True
    print(s.canJump(nums=[2, 5, 0, 0]))
    # True
    print(s.canJump(nums=[0]))
    # False
    print(s.canJump(nums=[0, 1]))
